package com.lej.core.utils

import android.content.Context
import android.os.Build
import com.qmuiteam.qmui.util.QMUIDeviceHelper
import java.io.BufferedReader
import java.io.FileReader

/**
 *  author: cjq
 *  date  : 2022/7/21 0021
 *  desc  : 低配置手机检测
 */
object DevicesUtils {
    private const val LOW_MEMORY = 1024 * 1024 * 1024 * 2.5F
    private const val LOW_MEMORY_6G = 1024 * 1024 * 1024 * 5.5F

    /**
     * 内存检测
     */
    fun Context.isLowConfiguration(): Boolean {
        // 屏蔽3G内存以下
        val memorySize = QMUIDeviceHelper.getTotalMemory(this)
        if (memorySize > 1 && memorySize < LOW_MEMORY) {
            return true
        }
        return isLowerCpu()
    }

    /**
     * 内存检测
     */
    fun Context.isLow6GConfiguration(): Boolean {
        // 屏蔽3G内存以下
        val memorySize = QMUIDeviceHelper.getTotalMemory(this)
        if (memorySize > 1 && memorySize < LOW_MEMORY_6G) {
            return true
        }
        return false
    }



    /**
     * 手机cpu检测
     */
    private fun getCpuName(): String {
        var cpuHardware = ""
        try {
            val fr = FileReader("/proc/cpuinfo")
            val br = BufferedReader(fr)
            var line: String
            try {
                while (br.readLine().also { line = it } != null) {
                    val result = line.lowercase()
                    val array = result.split(Regex(":\\s+"), 2).toTypedArray()
                    when {
                        array[0].startsWith("model name") -> {
                        }
                        array[0].startsWith("cpu part") -> {
                        }
                        array[0].startsWith("hardware") -> {
                            cpuHardware = array[1]
                        }
                        array[0].startsWith("bogomips") -> {
                        }
                        array[0].startsWith("features") -> {
                            //cpu硬件型号
                        }
                        array[0].startsWith("cpu implementer") -> {
                        }
                        array[0].startsWith("cpu architecture") -> {
                        }
                        array[0].startsWith("cpu variant") -> {
                        }
                    }
                }
            } catch (e: Exception) {
            }
        } catch (e: Exception) {
        }

        return cpuHardware
    }

    private fun isLowerCpu(): Boolean {
        val name = getCpuName()
        if(name.isEmpty()) return false
        // https://www.mydrivers.com/zhuanti/tianti/01/index_gaotong.html#xl600  骁龙630以下
        // MSM8953 Pro 骁龙626   , MSM8953 骁龙625  , MSM8952  骁龙617  , MSM8939 v2  骁龙616,  MSM8939 骁龙615, MT6757CD
        // Helio P25,  MT6763T Helio P23,  Helio P22  ,MT6757  Helio P20  , MT6755/6755M Helio P10  , Helio A22 ,MT6739
        val lowerCpuArray = arrayOf(
            "MSM8953 Pro",
            "MSM8953",
            "MSM8952",
            "MSM8939 v2",
            "MSM8939",
            "MT6757CD",
            "MT6763T",
            "MT6757",
            "MT6755",
            "MT6755M",
            "MT6739",
        )
        lowerCpuArray.forEach {
            if (name.endsWith(it, true)) {
                return true
            }
        }
        return false
    }
}